<?php
namespace app\admin\controller;
use app\common\CacheKey;
use app\common\service\Admin as AdminService;
use think\Cache;
use think\Controller;
use think\Request;

class Login extends Controller
{
    public function login(Request $request)
    {
        $username = $request->post("username");
        $password = $request->post("password");
        $service = new AdminService();
        $token = $service->login($username,$password,3600 * 2);
        if($token){
            $this->success("登录成功","",['token'=>$token]);
        }
        $this->error("登录失败");
    }

    /**
     * 这个方法没有必要，因为分离模式，退出只要前端操作就行了
     */
    public function logout(Request $request){
        $adminId = $request->header("adminId");
        Cache::clear(CacheKey::ADMIN.'#'.$adminId);
        $this->success("退出成功","");
    }
}
